package net.sf.graph4j.datastructure.disjointset.linkedlist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.graph4j.datastructure.disjointset.IDisjointSet;

/**
 * 
 */

/**
 * A simple way to implement a disjoint-set data structure is to represent each
 * set by a linked list. The first object in each linked list serves as its
 * set's representative. Each object in the linked list contains a set member, a
 * pointer to the object containing the next set member, and a pointer back to
 * the representative. Each list maintains pointers head, to the representative,
 * and tail, to the last object in the list. Figure 21.2(a) shows two sets.
 * Within each linked list, the objects may appear in any order (subject to our
 * assumption that the first object in each list is the representative).
 * 
 * @author chen cong
 * 
 */
public class DisjointSetImpl<E> implements IDisjointSet<E> {

	List<DisjointSetNode<E>> entry = new ArrayList<DisjointSetNode<E>>();

	private DisjointSetNode<E> head;
	
	private DisjointSetNode<E> tail;

	public DisjointSetImpl() {
		super();
	}

	public DisjointSetImpl(DisjointSetNode<E> head) {
		super();
		this.head = head;
		add(head);
	}

	public boolean add(DisjointSetNode<E> node) {
		return entry.add(node);
	}

	public Iterator<DisjointSetNode<E>> iterator() {
		return entry.iterator();
	}

	public int size() {
		return entry.size();
	}

	/**
	 * @param head
	 *            the head to set
	 */
	public void setHead(DisjointSetNode<E> head) {
		this.head = head;
	}

	/**
	 * @return the tail
	 */
	public DisjointSetNode<E> getTail() {
		return tail;
	}

	/**
	 * @param tail the tail to set
	 */
	public void setTail(DisjointSetNode<E> tail) {
		this.tail = tail;
	}

	/**
	 * @return the head
	 */
	public DisjointSetNode<E> getHead() {
		return head;
	}

}
